﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.IBLL;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.Web.Tests.Fakes
{
    public class FakeStatusManager : IStatusManager   
    {
        private List<Status> statusList;

        public FakeStatusManager(List<Status> statuses)
        {
            statusList = statuses;
        }

        /// <summary>
        /// Create a status (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Status NewStatus()
        {
            var status = new Status();
            
            status.CreateTime = DateTime.Now;            

            return status;
        }

        /// <summary>
        /// Save status
        /// </summary>
        /// <param name="status">Status entity</param>
        /// <returns></returns>
        public Status Save(Status status)
        {
            Validate(status);
            if (Get(status.StatusID) != null)
                throw new Exception("Existing status");
            status.StatusID = Guid.NewGuid();
            statusList.Add(status);

            return Get(status.StatusID);
        }

        /// <summary>
        /// Access to statuss
        /// </summary>
        /// <param name="statusId">Status ID</param>
        /// <returns></returns>
        public Status Get(Guid statusId)
        {
            return statusList.SingleOrDefault(u => u.StatusID == statusId);
        }

        /// <summary>
        /// Access to statuss
        /// </summary>
        /// <param name="statusId">Status ID</param>
        /// <returns></returns>
        public IList GetByIteration(string iterationId)
        {
            return statusList.FindAll(r => r.IterationID == iterationId);
        }      

        /// <summary>
        /// Delete Status
        /// </summary>
        /// <param name="statusId">Status ID</param>
        /// <returns></returns>
        public void Delete(Guid statusId)
        {
            var status = Get(statusId);
            if (status == null) throw new Exception("NotFound");
            statusList.Remove(status);
        }

        /// <summary>
        /// Update Status
        /// </summary>
        /// <param name="status">Status Entity</param>
        /// <returns></returns>
        public Status Update(Status status)
        {
            Status oldStatus = Get(status.StatusID);
            if (oldStatus != null)
                statusList.Remove(oldStatus);
            else
                status.StatusID = Guid.NewGuid();

            statusList.Add(status);
            return Get(status.StatusID);
        }

        /// <summary>
        /// Access to statuss
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return statusList;
        }

        private void Validate(Status status)
        {
            //TODO throw exception on error.
        }        
    }
}
